package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.commen.BaseContext;
import com.itheima.reggie.commen.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //处理登录校验
    @RequestMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String password = employee.getPassword();
        //将密码加密成MD5
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //wrapper.eq("实体类::查询字段", "条件值"); 相当于where条件
        queryWrapper.eq(Employee::getUsername, employee.getUsername());

        Employee emp = employeeService.getOne(queryWrapper);

        if (emp==null|| !(password.equals(emp.getPassword()))){
            //没有查询到的就返回失败
            return R.error("登录失败");
        }
        //判断用户是否被禁用
        if (emp.getStatus()==0){
            return R.error("用户被禁用");
        }

        //登录成功后将用户id添加到session里面
        request.getSession().setAttribute("employee",emp.getId());
        //将数据添加进返回结果
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //移除userid
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    //添加员工
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpServletRequest request){
        System.out.println("进入save方法");
      /*  //设置添加的时间，更新时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //设置创建人的信息及更新人(当前登录的用户)
        Long empId =(Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/

        //设置默认密码,进行加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //使用全局异常来处理添加时可能出现的异常情况？比如解决用户名冲突问题
        employeeService.save(employee);
        return R.success("添加成功");
    }

    //分页查询员工信息
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize, String name){
        log.info("page{},pageSize{},name{}",page,pageSize,name);
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        //进行条件分页
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    //修改账户信息，启用或禁用用户账户
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpServletRequest request){
        System.out.println("进入enableOrDisableEmployee===="+employee);
        //进行权限校验，防止非系统管理员修改用户状态
        Long emp =(Long) request.getSession().getAttribute("employee");
        if (employee.getStatus()!=null&&emp!=1){
            return R.error("更改失败，非法修改");
        }
        //更新用户信息
     /*   employee.setUpdateUser(emp);
        employee.setUpdateTime(LocalDateTime.now());*/
        employeeService.updateById(employee);
        return R.success("更改成功");
    }

    //根据id获取员工信息
    @GetMapping("/{id}")
    public R<Employee> queryEmployeeById(@PathVariable("id") Long id){
        Employee employee = employeeService.getById(id);
        //这样做的目的是：防止在查询时，该用户被删除
        if (employee!=null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }

  /*  @GetMapping("/page")
    public R<Page> page(@RequestParam("page") int page,@RequestParam("pageSize") int pageSize){
        //进行员工信息分页查询

    }*/
}
